import Vue from 'vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
import {Url, Error} from 'common/js/config'

Vue.use(VueAxios, axios)

// 新建话题
export function createTopic(accesstoken, title, tab, content) {
  return new Promise((resolve, reject) => {
    axios.post(`${Url}/topics`, {accesstoken: accesstoken, title: title, tab: tab, content: content})
      .then((response) => {
        resolve(response.data)
      })
      .catch((error) => {
        if (typeof error.response.data === 'object') {
          resolve(error.response.data)
        } else {
          resolve(Error)
        }
      })
  })
}

// 新建评论
export function createComment(accesstopken, topicId, content, replyId) {
  return new Promise((resolve, reject) => {
    axios.post(`${Url}/topic/${topicId}/replies`, {accesstoken: accesstopken, content: content, reply_id: replyId})
      .then((response) => {
        resolve(response.data)
      })
      .catch((error) => {
        if (typeof error.response.data === 'object') {
          resolve(error.response.data)
        } else {
          resolve(Error)
        }
      })
  })
}
